{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算完成x、y\n",
      "计算完成面积\n",
      "UUID 已成功更新！更新数为：'8'\n",
      "已成功删除字段: MOID, WTBH, WTMX, SQMC, JDMC, GID, FKSJ, EXISTSJZW, ObjectID\n"
     ]
    }
   ],
   "source": [
    "import arcpy\n",
    "import uuid\n",
    "\n",
    "# 获取当前工程\n",
    "aprx = arcpy.mp.ArcGISProject(\"CURRENT\")\n",
    "\n",
    "# 获取活动的地图或场景\n",
    "active_map_or_scene = aprx.activeMap\n",
    "# 查找对应名称的图层\n",
    "target_layer_name = \"梅川镇_要修改\"\n",
    "\n",
    "target_layer = None\n",
    "for lyr in active_map_or_scene.listLayers():\n",
    "    if lyr.name == target_layer_name:\n",
    "        target_layer = lyr\n",
    "        break\n",
    "\n",
    "if target_layer is None:\n",
    "    print(\"图层未找到。\")\n",
    "    exit\n",
    "\n",
    "geometry_property = [[\"x\", \"INSIDE_X\"], [\"y\", \"INSIDE_Y\"]]\n",
    "cgcs2000_sr = arcpy.SpatialReference(\"GCS_China_Geodetic_Coordinate_System_2000\")\n",
    "arcpy.management.CalculateGeometryAttributes(\n",
    "    target_layer,\n",
    "    geometry_property,\n",
    "    coordinate_system = cgcs2000_sr\n",
    ")\n",
    "\n",
    "print(\"计算完成x、y\")\n",
    "\n",
    "geometry_property = [[\"面积\", \"AREA\"]]\n",
    "unit = \"SQUARE_METERS\"\n",
    "cgcs2000_degree = arcpy.SpatialReference(\"CGCS2000_3_Degree_GK_CM_117E\")\n",
    "arcpy.management.CalculateGeometryAttributes(\n",
    "    target_layer,\n",
    "    geometry_property,\n",
    "    area_unit = unit,\n",
    "    coordinate_system = cgcs2000_degree\n",
    ")\n",
    "\n",
    "print(\"计算完成面积\")\n",
    "\n",
    "# 获取 uuid 字段\n",
    "uuid_field = [field for field in arcpy.ListFields(target_layer) if field.name == \"uuid\"][0]\n",
    "\n",
    "# 遍历图层中的所有记录\n",
    "count = 0\n",
    "with arcpy.da.UpdateCursor(target_layer, [\"uuid\"]) as cursor:\n",
    "    for row in cursor:\n",
    "        # 生成新的 UUID\n",
    "        new_uuid = str(uuid.uuid4())\n",
    "        \n",
    "        # 更新 uuid 字段\n",
    "        row[0] = new_uuid\n",
    "        \n",
    "        # 更新记录\n",
    "        cursor.updateRow(row)\n",
    "        count += 1\n",
    "\n",
    "print(f\"UUID 已成功更新！更新数为：'{count}'\" )\n",
    "\n",
    "if target_layer:\n",
    "    fields_to_keep = [\"FID\", \"Shape\", \"建筑顶\", \"建筑底\", \"建筑层\", \"uuid\", \"结构\", \"建筑高\", \"面积\", \"x\", \"y\", \"sqjcwhmc\"]\n",
    "\n",
    "    # 获取所有字段，并过滤出不需要保留的字段\n",
    "    all_fields = [field.name for field in arcpy.ListFields(target_layer)]\n",
    "    fields_to_delete = [field for field in all_fields if field not in fields_to_keep]\n",
    "\n",
    "    if fields_to_delete:\n",
    "        try:\n",
    "            # 删除不需要保留的字段\n",
    "            arcpy.management.DeleteField(target_layer, fields_to_delete)\n",
    "            print(f\"已成功删除字段: {', '.join(fields_to_delete)}\")\n",
    "        except arcpy.ExecuteError as e:\n",
    "            print(f\"发生错误: {e}\")\n",
    "    else:\n",
    "        print(\"没有需要删除的字段\")\n",
    "else:\n",
    "    print(\"图层未找到。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ArcGISPro",
   "language": "Python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".py",
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
